//
//  UIImagePickerController+YCExtension.m
//  YCPublicDemo
//
//  Created by 董知樾 on 2017/5/23.
//  Copyright © 2017年 xinhuanwangluo. All rights reserved.
//

#import "UIImagePickerController+YCExtension.h"
#import <objc/runtime.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface UIImagePickerController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (copy, nonatomic) void (^simpleComplate)(UIImage *image);

@property (strong, nonatomic) NSNumber *saveToAlbum;

@property (copy, nonatomic) void (^recordVideoComplate)(NSError *error, NSString *videoPath);

@end

@implementation UIImagePickerController (YCExtension)

+ (instancetype)pickImageForm:(UIImagePickerControllerSourceType)sourceType complate:(void (^)(UIImage *image))complate {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = sourceType;
    
    picker.simpleComplate = complate;
    picker.delegate = picker;
    [[UIApplication sharedApplication].keyWindow.rootViewController showDetailViewController:picker sender:nil];
    return picker;
}

+ (instancetype)takePhotoAndSaveToAlbum:(BOOL)save complate:(void (^)(UIImage *))complate {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    picker.simpleComplate = complate;
    picker.saveToAlbum = @(save);
    picker.delegate = picker;
    [[UIApplication sharedApplication].keyWindow.rootViewController showDetailViewController:picker sender:nil];
    return picker;
}

+ (instancetype)recordVideoAndSaveToAlbumComplate:(void (^)(NSError *error, NSString *videoPath))complate {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    picker.recordVideoComplate = complate;
    picker.saveToAlbum = @(YES);
    picker.delegate = picker;
    picker.mediaTypes = @[(__bridge id)kUTTypeMovie];
    [[UIApplication sharedApplication].keyWindow.rootViewController showDetailViewController:picker sender:nil];
    return picker;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
//    if (self.simpleComplate) {
//        self.simpleComplate(image);
//    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSLog(@"%@",info[UIImagePickerControllerReferenceURL]);
    if (self.simpleComplate) {
        
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera && picker.saveToAlbum.boolValue) {
            
            UIImageWriteToSavedPhotosAlbum(info[UIImagePickerControllerOriginalImage], self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
        } else if (info[UIImagePickerControllerEditedImage]) {
            
            self.simpleComplate(info[UIImagePickerControllerEditedImage]);
            [self dismissViewControllerAnimated:YES completion:nil];
            
        } else if (info[UIImagePickerControllerOriginalImage]) {
            
            self.simpleComplate(info[UIImagePickerControllerOriginalImage]);
            [self dismissViewControllerAnimated:YES completion:nil];
            
        } else if (info[UIImagePickerControllerReferenceURL]) {
//            self.simpleComplate(info[UIImagePickerControllerReferenceURL]);
        }
    }
    
    if (self.recordVideoComplate) {
        NSURL *url = info[UIImagePickerControllerMediaURL];
        UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, @selector(video:didFinishSavingWithError:contextInfo:), NULL);
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    if (self.simpleComplate) {
        self.simpleComplate(nil);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (self.simpleComplate) {
        self.simpleComplate(image);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (self.recordVideoComplate) {
        self.recordVideoComplate(error, videoPath);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)setSimpleComplate:(void (^)(UIImage *))simpleComplate {
    objc_setAssociatedObject(self, @selector(simpleComplate), simpleComplate, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(UIImage *))simpleComplate {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setSaveToAlbum:(NSNumber *)saveToAlbum {
    objc_setAssociatedObject(self, @selector(saveToAlbum), saveToAlbum, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)saveToAlbum {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setRecordVideoComplate:(void (^)(NSError *, NSString *))recordVideoComplate {
    objc_setAssociatedObject(self, @selector(recordVideoComplate), recordVideoComplate, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(NSError *, NSString *))recordVideoComplate {
    return objc_getAssociatedObject(self, _cmd);
}


@end
